﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.DAL.LocalDataAccess/WorkingInfoAccess
* 类       名: WorkingInfoAccess
* 功      能:  TD
* 时      间:  2024/5/5 16:00:38
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.Extensions;
using WaferSelectV3.Services;

namespace WaferSelectV3.DAL.LocalDataAccess
{
    public class WorkingInfoAccess
    {
        /// <summary>
        /// 获取当前作业信息,如果有多条作业信息,就显示第一条
        /// </summary>
        /// <returns></returns>
        public static DataResult<WorkingInfoEntity> GetWorkingInfo()
        {
            DataResult<WorkingInfoEntity> result = new DataResult<WorkingInfoEntity>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.WorkingInfos.FirstOrDefault();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="workingInfo"></param>
        /// <returns></returns>
        public static DataResult AddWorkingInfo(WorkingInfoEntity workingInfo)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.WorkingInfos.Add(workingInfo);
                    int affectedRows = context.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantProvider.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="workingInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static DataResult UpdateWorkingInfo(WorkingInfoEntity workingInfo)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    // 查询要更新的数据是否存在
                    var oldEntity = context.WorkingInfos.FirstOrDefault(w => w.Id == workingInfo.Id);
                    if (oldEntity != null)
                    {
                        context.Entry(oldEntity).CurrentValues.SetValues(workingInfo);
                        int affectRows = context.SaveChanges();
                        if (affectRows >= 0)
                        {
                            result.State = true;
                        }
                        else
                        {
                            result.Message = ConstantProvider.DatabaseUpdateError;
                        }
                    }
                    else
                    {
                        result.Message = $"更新失败,要更新的数据不存在,更新的数据Id = {workingInfo.Id},更新的数据表为 = {nameof(context.WorkingInfos)}";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
